
<?php 
Yii::app()->clientScript->registerCssFile('css/InvoiceReport.css');
$this->layout = '//layout/space';
?>
<script>
    $(document).ready(function(){
        $('.paymentSpan').parent().parent().addClass('payment');
    })
</script>
<div id="invoice-report">
    <?php if(!$noHeader):?>
        <div id="title">
            QSI, INC<br/>
            ACCOUNT STATEMENT<br/>
            DATE <?php echo date('Y-m-d'); ?> 
            
        </div>
    <?php endif;?>
    
    <div id="content">
        <h3>
            <?php 
            if($customerModel!=null){
                echo $customerModel->custno.' - '.$companyName; 
            }
            ?>
        </h3>
        <b>Invoices</b>
        <?php
        $invoiceTable =new CI_Table();
        $invoiceTable->set_template(array(
            'table_open'=>'<table class="">'
        ));
        $invoiceTable->set_heading(array(
            'Type','Job No','Contract No','LocNo','Dept','Date','No.','Amount','Balance'
        ));
        foreach($invoiceData as $invoice){
           
            $invoiceTable->add_row(array(
                'I',
                $invoice['jobnumber'],
                $invoice['agreementno'],
                $invoice['locno'],
                $invoice['department'],
                $invoice['date'],
                $invoice['invoice'],
                number_format($invoice['amountcharge'],2),
                number_format($invoice['balance'],2)                
            ));
            $i=0;
                        
            foreach( $invoice['payments'] as $payment){
             
                $invoiceTable->add_row(array(
                    'N - '.$payment['id'].'<span class="paymentSpan"></span>',
                    '',
                    '',
                    '',
                    '',
                    $payment['date'],
                    '',
                    number_format($payment['paid'],2),
                    ''
                ));                
            }
            
        }
        $invoiceTable->add_row(array(
            array(
                'data'=>'Total Due',
                'colspan'=>8
            ),
            $totalDueOfInvoice
        ));
        echo $invoiceTable->generate();
        ?>
        <br/>
        <b>Sales Order</b>        
        <?php 
            $salesOrderTable = new CI_Table();
            $salesOrderTable->set_heading(array(
                'Type',
                'Job No',
                'Contract No',
                'LocNo',
                'Dept',
                'Date',
                'No.',
                'Deposits',
                'Deposit owed',
                'Invoiced | Amt'
            ));
            
            foreach($salesOrderData as $salesOrder){
                $invoiceHtml ='';
                foreach($salesOrder['invoices'] as $invoice){
                    $invoiceHtml.= $invoice['invoice']. ' | '. number_format($invoice['amountcharge'],2) . '<br/>';
                }
                $salesOrderTable->add_row(array(
                    'S',
                    $salesOrder['jobnumber'],
                    $salesOrder['agreementno'],
                    $salesOrder['locno'],
                    $salesOrder['department'],
                    $salesOrder['date'],
                    $salesOrder['invoice'],
                    $salesOrder['paymentTotal'],
                    number_format($salesOrder['balance'],2),
                    $invoiceHtml
                ));
                foreach($salesOrder['payments'] as $payment){
                    $salesOrderTable->add_row(array(
                        'Dep P-'.$payment['id'] . '<span class="paymentSpan"></span>',
                        '',
                        '',
                        '',
                        '',
                        $payment['date'],
                        '',
                        number_format($payment['paid'],2),
                        '',
                        ''
                    ));
                }
            }
            $salesOrderTable->add_row(array(
                array(
                    'data'=>'Sales Order Total',
                    'colspan'=>'7'
                ),
                number_format($totalSalesOrder,2),
                '',
                ''
            ));
            
            $salesOrderTable->add_row(array(
                array(
                    'data'=>'Deposit Total',
                    'colspan'=>'7'
                ),
                number_format($totalDeposit,2),
                '',
                ''
            ));
            
            $salesOrderTable->add_row(array(
                array(
                    'data'=>'Not Invoiced',
                    'colspan'=>'9'
                ),
                number_format($totalUnInvoiced,2)
            ));
            
            echo $salesOrderTable->generate();
        ?>
        <br/>
        <?php
        $summaryTable = new CI_Table();
        $summaryTable->set_template(array(
            'table_open'=>'<table width = 500>'
        ));
        $summaryTable->add_row(array(
            'Invoice Total',number_format($totalInvoice,2)
        ));
        $summaryTable->add_row(array(
            'Sales Order Total',number_format($customerSalesOrderTotal,2)
        ));
        $summaryTable->add_row(array(
            'Deposit Total',number_format($totalDeposit,2)
        ));
        
        echo $summaryTable->generate();
        ?>
    </div>
</div>
